package com.gzhryc.shared_device.oem.code.agent.dao;

import com.gzhryc.jdbc.BaseDao;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.Query;
import com.gzhryc.shared_device.oem.code.agent.dao.db.AgentRelation;
import com.gzhryc.shared_device.oem.code.agent.dao.models.AgentRelationCount;

import java.sql.SQLException;
import java.util.List;

/**
 * 代理关系
 */
public class AgentRelationDao extends BaseDao<AgentRelation> {

    public AgentRelationDao(String jdbcKey){
        super(jdbcKey);
    }

    public List<AgentRelationCount> countGroupUserId(Conditions conditions,Integer pageNo,Integer pageLimit) throws SQLException {
        String sql = "SELECT t1.* FROM (SELECT t.user_id AS userId,COUNT(1) AS relationUserCount FROM " + this.tableName
                + " t INNER JOIN user_info user ON t.user_id = user.id";
        Query query = new Query(sql);
        query.appendSql(conditions.toSql(query.getParams(),"t",true));
        query.appendSql(" GROUP BY user_id) t1");
        query.setPageNo(pageNo);
        query.setPageLimit(pageLimit);
        return this.jdbcHelper.find(query,AgentRelationCount.class);
    }

    public Long countUserId(Conditions conditions) throws SQLException {
        String sql = "SELECT COUNT(DISTINCT t.user_id) FROM " + this.tableName + " t INNER JOIN user_info user ON t.user_id = user.id";
        Query query = new Query(sql);
        query.appendSql(conditions.toSql(query.getParams(),"t",true));
        return this.jdbcHelper.getFirstField(query,Long.class);
    }
}
